import { Points } from "./types";
import { getRandom } from "./util";
import { SquareGrounp } from "./SquareGrounp";

/**
 *    --
 * -- -- --
 */
export class TShape extends SquareGrounp {
  constructor(
    _centerPoint: Points,
    _color: string) {
    super(
      [{ x: -1, y: 0 }, { x: 0, y: 0 }, { x: 1, y: 0 }, { x: 0, y: -1 }],
      _centerPoint, _color);
  }
}

/**
 *       --
 * -- -- --
 */
export class LShape extends SquareGrounp {
  constructor(
    _centerPoint: Points,
    _color: string) {
    super(
      [{ x: -2, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 0 }, { x: 0, y: -1 }],
      _centerPoint, _color);
  }
}

/**
 * --      
 * -- -- --
 */
export class LMirrorShape extends SquareGrounp {
  constructor(
    _centerPoint: Points,
    _color: string) {
    super(
      [{ x: 2, y: 0 }, { x: 1, y: 0 }, { x: 0, y: 0 }, { x: 0, y: -1 }],
      _centerPoint, _color);
  }
}

/**
 *    -- --    
 * -- -- 
 */
export class SShape extends SquareGrounp {
  constructor(
    _centerPoint: Points,
    _color: string) {
    super(
      [{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 0, y: 1 }, { x: -1, y: 1 }],
      _centerPoint, _color);
  }

  rotate() {
    // 顺时针转一次，逆时针转一次
    super.rotate();
    this.isClock = !this.isClock;
  }
}

/**
 *  -- --    
 *     -- --
 */
export class SMirrorShape extends SquareGrounp {
  constructor(
    _centerPoint: Points,
    _color: string) {
    super(
      [{ x: 0, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 1 }, { x: 1, y: 1 }],
      _centerPoint, _color);
  }

  rotate() {
    // 顺时针转一次，逆时针转一次
    super.rotate();
    this.isClock = !this.isClock;
  }
}

/**
 *  -- --    
 *  -- --
 */
export class SquareShape extends SquareGrounp {

  constructor(
    _centerPoint: Points,
    _color: string) {
    super(
      [{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 0, y: 1 }, { x: 1, y: 1 }],
      _centerPoint, _color);
  }
  // 不要旋转，直接返回原有形状
  afterRotateShape() {
    return this.shape;
  }
}

/**
 *  -- -- -- --
 */
export class LineShape extends SquareGrounp {

  constructor(
    _centerPoint: Points,
    _color: string) {
    super(
      [{ x: -1, y: 0 }, { x: 0, y: 0 }, { x: 1, y: 0 }, { x: 2, y: 0 }],
      _centerPoint, _color);
  }

  rotate() {
    // 顺时针转一次，逆时针转一次
    super.rotate();
    this.isClock = !this.isClock;
  }
}

/**
 *  --    --
 *  -- -- -- 
 */
export class UShape extends SquareGrounp {

  constructor(
    _centerPoint: Points,
    _color: string) {
    super(
      [{ x: -1, y: 0 }, { x: 0, y: 0 }, { x: 1, y: 0 }, { x: -1, y: -1 }, { x: 1, y: -1 }],
      _centerPoint, _color);
  }
}

export const shapes = [
  TShape,
  LShape,
  LMirrorShape,
  SShape,
  SMirrorShape,
  SquareShape,
  LineShape,
  UShape
]

export const colors = [
  "red",
  "green",
  "blue",
  "orange"
]

/**
 * 随机产生一个俄罗斯方块（颜色随机、形状随机）
 * @param centerPoint 
 */
export function createTeris(centerPoint: Points): SquareGrounp {
  let index = getRandom(0, shapes.length);
  const shape = shapes[index];
  index = getRandom(0, colors.length);
  const color = colors[index];
  return new shape(centerPoint, color) as SquareGrounp;
}